<Project Sdk="Microsoft.Build.NoTargets">

  <PropertyGroup>
    <TargetFramework>$(NetCoreAppCurrent)-$(TargetOS)</TargetFramework>
    <!-- By default, build the NetCoreAppCurrent vertical only. -->
    <BuildTargetFramework Condition="'$(BuildAllConfigurations)' != 'true'">$(NetCoreAppCurrent)</BuildTargetFramework>
     <!-- Import the illink file which contains some of the logic required to illink the out-of-band assemblies. -->
    <ExplicitlyImportCustomILLinkTargets Condition="'$(BuildTargetFramework)' == '$(NetCoreAppCurrent)' or '$(BuildTargetFramework)' == ''">true</ExplicitlyImportCustomILLinkTargets>
  </PropertyGroup>

  <ItemGroup Condition="'$(BuildTargetFramework)' == '$(NetCoreAppCurrent)' or '$(BuildTargetFramework)' == ''">
    <ProjectReference Include="sfx-src.proj" OutputItemType="SharedFrameworkAssembly" />
    <ProjectReference Include="oob-src.proj" OutputItemType="OOBAssembly" />

    <ProjectReference Include="apicompat\ApiCompat.proj"
                      Condition="'$(DotNetBuildSourceOnly)' != 'true' and '$(ApiCompatValidateAssemblies)' != 'false'" />
  </ItemGroup>

  <ItemGroup Condition="'$(BuildTargetFramework)' != '$(NetCoreAppCurrent)' or '$(BuildTargetFramework)' == ''">
    <ProjectReference Include="oob-all.proj" SkipGetTargetFrameworkProperties="true" />
  </ItemGroup>

  <!-- Support building reference projects only. -->
  <ItemGroup Condition="'$(RefOnly)' == 'true'">
    <ProjectReference Remove="@(ProjectReference)" />
    <ProjectReference Include="sfx-ref.proj" OutputItemType="SharedFrameworkAssembly" />
    <ProjectReference Include="oob-ref.proj" />
  </ItemGroup>

  <Target Name="GetTrimOOBAssembliesInputs"
          DependsOnTargets="ResolveProjectReferences">
    <PropertyGroup>
      <OOBAssembliesTrimDir>$([MSBuild]::NormalizeDirectory('$(IntermediateOutputPath)', 'illink-$(TargetArchitecture)'))</OOBAssembliesTrimDir>
      <OOBAssembliesTrimMarkerFile>$(OOBAssembliesTrimDir)marker.txt</OOBAssembliesTrimMarkerFile>
    </PropertyGroup>

    <ItemGroup>
      <OOBLibrarySuppressionsXml Include="@(OOBAssembly->Metadata('ILLinkSuppressionsXmls'))" />

      <OOBAssemblyToTrim Include="@(OOBAssembly->WithMetadataValue('IsTrimmable', 'true'))" />
      <OOBAssemblyReference Include="@(OOBAssembly->WithMetadataValue('IsTrimmable', 'false'));
                                     @(SharedFrameworkAssembly)" />
    </ItemGroup>
  </Target>

  <Target Name="TrimOOBAssemblies"
          AfterTargets="Build"
          Condition="'$(RefOnly)' != 'true' and '@(OOBAssembly)' != ''"
          DependsOnTargets="GetTrimOOBAssembliesInputs;PrepareForAssembliesTrim"
          Inputs="$(ILLinkTasksAssembly);@(OOBAssemblyToTrim);@(OOBAssemblyReference);@(OOBLibrarySuppressionsXml)"
          Outputs="$(OOBAssembliesTrimMarkerFile)">
    <Message Text="$(MSBuildProjectName) -> Trimming $(OutputRID) out-of-band assemblies with ILLinker..." Importance="high" />

    <PropertyGroup>
      <OOBILLinkArgs>$(ILLinkArgs)</OOBILLinkArgs>
      <!-- Unnecessary suppressions - disable for now since we didn't clean the runtime yet -->
      <OOBILLinkArgs>$(ILLinkArgs) --nowarn IL2121</OOBILLinkArgs>
      <OOBILLinkArgs Condition="'@(OOBLibrarySuppressionsXml)' != ''" >$(OOBILLinkArgs) --link-attributes "@(OOBLibrarySuppressionsXml->'%(FullPath)', '" --link-attributes "')"</OOBILLinkArgs>
    </PropertyGroup>

    <MakeDir Directories="$(OOBAssembliesTrimDir)" />
    <ILLink AssemblyPaths=""
        RootAssemblyNames="@(OOBAssemblyToTrim)"
        ReferenceAssemblyPaths="@(OOBAssemblyReference)"
        OutputDirectory="$(OOBAssembliesTrimDir)"
        ExtraArgs="$(OOBILLinkArgs)"
        ToolExe="$(_DotNetHostFileName)"
        ToolPath="$(_DotNetHostDirectory)" />

    <!-- Create a marker file which serves as the target's output to enable incremental builds. -->
    <Touch Files="$(OOBAssembliesTrimMarkerFile)"
           AlwaysCreate="true" />
  </Target>

  <Target Name="SetAzureDevOpsVariableForBuiltPackages"
          Condition="'$(ContinuousIntegrationBuild)' == 'true'"
          AfterTargets="Build;Pack">
    <Message Condition="Exists('$(ArtifactsDir)packages')" Importance="High" Text="##vso[task.setvariable variable=_librariesBuildProducedPackages]true" />
  </Target>

</Project>
